<?php
/**
 * AdMinLang is a class used for translations in AdMin
 *
 * Here is an example of usage
 * <code>
 * require_once(DIR_CLASSES ."/AdMinLang.php");
 *
 * $options = array("type" => "mySql", "dbLink" => $dbLink, "saveFile" => true);
 * $lang = new Lang($options);
 * $lang->translate("hello_world");
 * </code>
 *
 * @package AdMin Framework
 */
class Lang
{
    public $engine;
    public $lang = '';
    private $sessionManager;
    private $sessionKey = '';

    public function __construct($options)
    {
        $this->sessionManager = new SessionManager();
        $engineBuilder = new LangEngineBuilder();
        $this->engine = $engineBuilder->build($options);
        $this->sessionKey = '__lang_'. base64_encode($GLOBALS["CONF"]["url"]["root"]) .'__';
    }

    public function setLang($lang)
    {
        $this->lang = $lang;
        $this->sessionManager->set($this->sessionKey, $this->lang);
    }

    public function translate($wordID, $type = 'word', $lang = NULL)
    {
        $lang = $lang? $lang : $this->lang;
        $this->engine->changeType($type);
        $translation = $this->engine->getWord($wordID, $lang);
        return $translation;
    }

    public function getLang()
    {
        if(!$this->lang)
        {
            if(!$this->lang = $this->sessionManager->get($this->sessionKey))
                $this->setLang(($GLOBALS["CONF"]["lang"]["default"]? $GLOBALS["CONF"]["lang"]["default"] : 'es')); // backward compatibility
        }
    }
}

?>
